/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	timer.c
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-06-24
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "timer.h"
#include "log_console.h"
#ifdef RTOS
#include <rtthread.h>
#include <rthw.h>
#endif

/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/

 
/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/

__IO uint64_t LocalTime = 0;
/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/
//static TIM_HandleTypeDef TIM3_Handler;
//static timer_callback_node_t timer_cb_list={0};
hw_timer_t TIM_1K = {.tim = TIM3 , .node_callback = NULL, .cnt = 0};
hw_timer_t TIM_200K = {.tim = TIM2 , .node_callback = NULL, .cnt = 0};
/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/

void bsp_timer_init(void)
{
	__HAL_RCC_TIM3_CLK_ENABLE();
	TIM_1K.hal_handler.Instance=TIM_1K.tim;
	TIM_1K.hal_handler.Init.Prescaler=200-1;
	TIM_1K.hal_handler.Init.CounterMode=TIM_COUNTERMODE_UP;
	TIM_1K.hal_handler.Init.Period=1000-1;
	TIM_1K.hal_handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
	HAL_TIM_Base_Init(&TIM_1K.hal_handler);
	HAL_NVIC_SetPriority(TIM3_IRQn,1,1);
	HAL_NVIC_EnableIRQ(TIM3_IRQn);
	__HAL_TIM_ENABLE_IT(&TIM_1K.hal_handler,TIM_IT_UPDATE);
	__HAL_TIM_ENABLE(&TIM_1K.hal_handler);

	__HAL_RCC_TIM2_CLK_ENABLE();
	TIM_200K.hal_handler.Instance=TIM_200K.tim;
	TIM_200K.hal_handler.Init.Prescaler=20-1;
	TIM_200K.hal_handler.Init.CounterMode=TIM_COUNTERMODE_UP;
	TIM_200K.hal_handler.Init.Period=50-1;
	TIM_200K.hal_handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
	HAL_TIM_Base_Init(&TIM_200K.hal_handler);
	HAL_NVIC_SetPriority(TIM2_IRQn,1,1);
	HAL_NVIC_EnableIRQ(TIM2_IRQn);
	__HAL_TIM_ENABLE_IT(&TIM_200K.hal_handler,TIM_IT_UPDATE);
	__HAL_TIM_ENABLE(&TIM_200K.hal_handler);
}
void bsp_timer_deinit(void)
{
	HAL_NVIC_DisableIRQ(TIM3_IRQn);
	HAL_NVIC_DisableIRQ(TIM2_IRQn);
	__HAL_TIM_DISABLE_IT(&TIM_200K.hal_handler,TIM_IT_UPDATE);
	__HAL_TIM_DISABLE(&TIM_200K.hal_handler);
	__HAL_TIM_DISABLE_IT(&TIM_1K.hal_handler,TIM_IT_UPDATE);
	__HAL_TIM_DISABLE(&TIM_1K.hal_handler);
}
void TIM3_IRQHandler(void)
{
	timer_callback_node_t* p_cb_next;
	if(__HAL_TIM_GET_FLAG(&TIM_1K.hal_handler,TIM_IT_UPDATE))
	{
		__HAL_TIM_CLEAR_FLAG(&TIM_1K.hal_handler,TIM_IT_UPDATE);
		LocalTime += 1;
		TIM_1K.cnt++;
		p_cb_next=TIM_1K.node_callback;
		while(p_cb_next!=NULL&&p_cb_next->timer_callback!=NULL)
		{
			p_cb_next->timer_callback(&TIM_1K);
			p_cb_next=p_cb_next->next;
		}
	}
}
void TIM2_IRQHandler(void)
{
	timer_callback_node_t* p_cb_next;
	if(__HAL_TIM_GET_FLAG(&TIM_200K.hal_handler,TIM_IT_UPDATE))
	{
		__HAL_TIM_CLEAR_FLAG(&TIM_200K.hal_handler,TIM_IT_UPDATE);
		TIM_200K.cnt++;
		p_cb_next=TIM_200K.node_callback;
		while(p_cb_next!=NULL&&p_cb_next->timer_callback!=NULL)
		{
			p_cb_next->timer_callback(&TIM_200K);
			p_cb_next=p_cb_next->next;
		}
	}
}
void bsp_timer_callback_register(hw_timer_t* handle,timer_callback pfn_callback)
{
#ifdef RTOS
	rt_base_t it = rt_hw_interrupt_disable();
#else
	__disable_irq();
#endif
	timer_callback_node_t* p_new;
	timer_callback_node_t* p_next=handle->node_callback;
//	if(handle->node_callback==NULL)
//	{
//		timer_cb_list.node_callback=pfn_callback;
//#ifdef RTOS
//	rt_hw_interrupt_enable(it);
//#else
//	__enable_irq();
//#endif
//		return;
//	}
	while(p_next!=NULL && p_next->next!=NULL)
	{
		/*当前回调函数已经注册*/
		if(p_next->timer_callback == pfn_callback)
		{
#ifdef RTOS
			rt_hw_interrupt_enable(it);
#else
			__enable_irq();
#endif
			return;
		}
		p_next=p_next->next;
	}
	p_new=malloc(sizeof(timer_callback_node_t));
	p_new->next=handle->node_callback;
	p_new->timer_callback = pfn_callback;
	handle->node_callback = p_new;
#ifdef RTOS
	rt_hw_interrupt_enable(it);
#else
	__enable_irq();
#endif
}
uint32_t HAL_GetTick(void)
{
  return LocalTime;
}
#ifdef BOOTLOADER
void SysTickHandler(void)
{
	return;
}
#endif